import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Pages from "vite-plugin-pages";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

import path from "path";

export default defineConfig({
  base: "/",
  // publicDir: "public",
  resolve: {
    alias: [
      {
        find: "@",
        replacement: path.resolve(__dirname, "src"),
      },
    ], //src文件夹用@替代
  },
  plugins: [
    vue(),
    Pages({
      // 需要生成路由的文件目录，默认就是识别src下面的views文件
      dirs: [path.resolve(__dirname, "./src/views")],
      // 排除在外的目录，即不将所有 components 目录下的 .vue 文件生成路由
      exclude: ["**/components/*.vue"],
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: "sass",
        }),
      ],
    }),
  ],

  server: {
    host: "0.0.0.0",
    // allowedHosts: ["c11f-14-212-72-200.ngrok-free.app"],
    // allowedHosts: 'all', // 允许所有域名访问
    // port: 5000,
    open: true,
    cors: true, // 启用跨域支持（可选）
    proxy: {
      "/dev-api": {
        target: "http://8.130.158.147:8080", // 您的 ngrok 地址
        changeOrigin: true,
        secure: false, // 关闭 HTTPS 证书验证
        headers: {
          // 代理添加 ngrok 头
          "ngrok-skip-browser-warning": "true",
        },
        rewrite: (p) => p.replace(/^\/dev-api/, ""),
      },
      "/prod-api": {
        target: "http://8.130.158.147:8080",
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/prod-api/, ""),
      },
    },
  },
});
